1
2


第一章课程介绍

第二章开发环境的安装预配置讲解

第三章数据库表设计

image-20190118112306843

image-20190118202842370


image-20190118202856316


image-20190118203020004


image-20190118203238463


image-20190118203312065


image-20190118203404425

image-20190118203536661


image-20190118203726775


image-20190118203746271


image-20190118204022200


image-20190118204041111

image-20190118204153890

image-20190118204203627

image-20190118204308240

image-20190118204329534

mysql 索引怎么弄

普通索引

创建索引

这是最基本的索引,它没有任何限制。它有以下几种创建方式:

1
CREATE INDEX indexName ON mytable(username(length));

如果是CHAR,VARCHAR类型,length可以小于字段实际长度;如果是BLOB和TEXT类型,必须指定 length。

修改表结构(添加索引)

1
ALTER table tableName ADD INDEX indexName(columnName)

创建表的时候直接指定

1
2
3
4
5
6
7
8
9
CREATE TABLE mytable(  

ID INT NOT NULL,

username VARCHAR(16) NOT NULL,

INDEX [indexName] (username(length))

);

删除索引的语法

1
DROP INDEX [indexName] ON mytable;

唯一索引

它与前面的普通索引类似,不同的就是:索引列的值必须唯一,但允许有空值。如果是组合索引,则列值的组合必须唯一。它有以下几种创建方式:

创建索引

1
CREATE UNIQUE INDEX indexName ON mytable(username(length))

修改表结构

1
ALTER table mytable ADD UNIQUE [indexName] (username(length))

创建表的时候直接指定

1
2
3
4
5
6
7
8
9
CREATE TABLE mytable(  

ID INT NOT NULL,

username VARCHAR(16) NOT NULL,

UNIQUE [indexName] (username(length))

);

第四章 项目初始化

image-20190118204806760

image-20190118204820954

image-20190118204835790

image-20190118204853935

image-20190118204914129

image-20190118204932983

image-20190118204953922

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>mmall</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>mmall Maven Webapp</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>

<org.springframework.version>4.0.0.RELEASE</org.springframework.version>
<org.mybatis.version>3.4.1</org.mybatis.version>
<org.mybatis.spring.version>1.3.0</org.mybatis.spring.version>
</properties>

<dependencies>

<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>7.0.64</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
</dependency>


<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.3</version>
</dependency>

<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${org.mybatis.spring.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${org.mybatis.version}</version>
</dependency>

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.12</version>
</dependency>

<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
<!--<scope>runtime</scope>-->
</dependency>


<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.2</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>


<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>


<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>


<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<!--<scope>test</scope>-->
</dependency>

<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.3</version>
</dependency>


<!-- id加密解密 -->
<dependency>
<groupId>org.hashids</groupId>
<artifactId>hashids</artifactId>
<version>1.0.1</version>
</dependency>


<!-- ftpclient -->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.1</version>
</dependency>

<!-- file upload -->

<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.0.1</version>
</dependency>




<!-- mybatis pager -->

<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.0</version>
</dependency>

<dependency>
<groupId>com.github.miemiedev</groupId>
<artifactId>mybatis-paginator</artifactId>
<version>1.2.17</version>
</dependency>

<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>0.9.4</version>
</dependency>


<!-- alipay -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
</dependency>

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>


<build>
<finalName>mmall</finalName>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>

<!-- geelynote maven的核心插件之-complier插件默认只支持编译Java 1.4,因此需要加上支持高版本jre的配置,在pom.xml里面加上 增加编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<extdirs>${project.basedir}/src/main/webapp/WEB-INF/lib</extdirs>
</compilerArguments>
</configuration>
</plugin>
</plugins>

</build>


</project>

image-20190118205216808


时间戳 now ()

image-20190118210057273

image-20190118210233830

image-20190118210455052

image-20190118210603020


mybatis-plugin

高清小鸟大图 🐦

Spring 官网代码 宠物医院。绿房子

https://github.com/spring-projects/spring-mvc-showcase

https://github.com/spring-projects/spring-petclinic

https://github.com/spring-projects/greenhouse

代码配置

配置

web.xml配置
1.设置过滤器 解决中文乱码问题
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!--设置过滤器 解决中文乱码问题-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!--拦截所有的请求 走这个filter-->
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2 监听器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!--监听器1 负责监听web容器的启动和关闭 RequestContextListener-->
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!--监听器2 将Spring和web容器整合的监听器 ContextLoaderListener-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--通过ContextLoaderListener加载applicationContext.xml-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
3 spring mvc的配置
1
2
3
4
5
6
7
8
9
10
11
12
13
<!--SpringMVC 的配置-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--优先级启动顺序-->
<load-on-startup>1</load-on-startup>
</servlet>


<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">

<display-name>Archetype Created Web Application</display-name>
<!--设置过滤器 解决中文乱码问题-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!--拦截所有的请求 走这个filter-->
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!--监听器1 负责监听web容器的启动和关闭 RequestContextListener-->
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!--监听器2 将Spring和web容器整合的监听器 ContextLoaderListener-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--通过ContextLoaderListener加载applicationContext.xml-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<!--SpringMVC 的配置-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--优先级启动顺序-->
<load-on-startup>1</load-on-startup>
</servlet>


<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

</web-app>

Spring的配置

1. Spring的主配置
1
2
3
4
5
6
7
<!-- 扫描的基本包路径 -->
<!-- 是否激活属性注入注解 -->
<context:component-scan base-package="com.mmall" annotation-config="true"/>
<!--<context:annotation-config/>-->
<aop:aspectj-autoproxy/>
<!--分离Spring配置文件-->
<import resource="applicationContext-datasource.xml"/>
2.Spring数据库的配置
2.1 配置Spring文件的时候将常量进行分离
1
2
3
4
5
6
7
8
9
10
11
12
<!--配置Spring文件的时候将常量进行分离-->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="2"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath:datasource.properties</value>
</list>
</property>
<property name="fileEncoding" value="utf-8"/>
</bean>
2.2 数据库连接池
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${db.driverClassName}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="${db.initialSize}"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="${db.maxActive}"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="${db.maxIdle}"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="${db.minIdle}"/>
<!-- 最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制 -->
<property name="maxWait" value="${db.maxWait}"/>
<!--#给出一条简单的sql语句进行验证 -->
<!--<property name="validationQuery" value="select getdate()" />-->
<property name="defaultAutoCommit" value="${db.defaultAutoCommit}"/>
<!-- 回收被遗弃的(一般是忘了释放的)数据库连接到连接池中 -->
<!--<property name="removeAbandoned" value="true" />-->
<!-- 数据库连接过多长时间不用将被视为被遗弃而收回连接池中 -->
<!--<property name="removeAbandonedTimeout" value="120" />-->
<!-- #连接的超时时间,默认为半小时。 -->
<property name="minEvictableIdleTimeMillis" value="${db.minEvictableIdleTimeMillis}"/>

<!--# 失效检查线程运行时间间隔,要小于MySQL默认-->
<property name="timeBetweenEvictionRunsMillis" value="40000"/>
<!--# 检查连接是否有效-->
<property name="testWhileIdle" value="true"/>
<!--# 检查连接有效性的SQL语句-->
<property name="validationQuery" value="SELECT 1 FROM dual"/>
</bean>
2.3 myBatis的配置
1
2
3
4
5
6
7
<!--mybatis的配置sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--mybatis的配置 读取mapper.xml文件-->
<property name="mapperLocations" value="classpath*:mappers/*Mapper.xml">

</property>
1
2
3
4
<!--mybatis的扫描Dao层 MapperScannerConfigurer-->
<bean name="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mmall.dao"/>
</bean>
2.4 分页插件
1
2
3
4
5
6
7
8
9
10
11
12
<!-- 分页插件 -->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>
dialect=mysql
</value>
</property>
</bean>
</array>
</property>
2.5 @Transaction
1
2
3
4
5
6
7
<!-- 使用@Transactional进行声明式事务管理需要声明下面这行 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<!-- 事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
<property name="rollbackOnCommitFailure" value="true"/>
</bean>

application.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 扫描的基本包路径 -->
<!-- 是否激活属性注入注解 -->
<context:component-scan base-package="com.mmall" annotation-config="true"/>
<!--<context:annotation-config/>-->
<aop:aspectj-autoproxy/>
<!--分离Spring配置文件-->
<import resource="applicationContext-datasource.xml"/>


</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

<context:component-scan base-package="com.mmall" annotation-config="true"/>
<!--配置Spring文件的时候将常量进行分离-->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="2"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath:datasource.properties</value>
</list>
</property>
<property name="fileEncoding" value="utf-8"/>
</bean>


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${db.driverClassName}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="${db.initialSize}"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="${db.maxActive}"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="${db.maxIdle}"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="${db.minIdle}"/>
<!-- 最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制 -->
<property name="maxWait" value="${db.maxWait}"/>
<!--#给出一条简单的sql语句进行验证 -->
<!--<property name="validationQuery" value="select getdate()" />-->
<property name="defaultAutoCommit" value="${db.defaultAutoCommit}"/>
<!-- 回收被遗弃的(一般是忘了释放的)数据库连接到连接池中 -->
<!--<property name="removeAbandoned" value="true" />-->
<!-- 数据库连接过多长时间不用将被视为被遗弃而收回连接池中 -->
<!--<property name="removeAbandonedTimeout" value="120" />-->
<!-- #连接的超时时间,默认为半小时。 -->
<property name="minEvictableIdleTimeMillis" value="${db.minEvictableIdleTimeMillis}"/>

<!--# 失效检查线程运行时间间隔,要小于MySQL默认-->
<property name="timeBetweenEvictionRunsMillis" value="40000"/>
<!--# 检查连接是否有效-->
<property name="testWhileIdle" value="true"/>
<!--# 检查连接有效性的SQL语句-->
<property name="validationQuery" value="SELECT 1 FROM dual"/>
</bean>
<!--mybatis的配置-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath*:mappers/*Mapper.xml">

</property>

<!-- 分页插件 -->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>
dialect=mysql
</value>
</property>
</bean>
</array>
</property>

</bean>
<!--mybatis的扫描Dao层-->
<bean name="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mmall.dao"/>
</bean>

<!-- 使用@Transactional进行声明式事务管理需要声明下面这行 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<!-- 事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
<property name="rollbackOnCommitFailure" value="true"/>
</bean>


</beans>

Spring MVC 的配置

1 编码配置
1
2
3
4
5
6
7
8
9
<!--配置编码-->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
2 JSON配置
1
2
3
4
5
6
7
8
<!--配置JSON格式-->
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
3 文件上传配置
1
2
3
4
5
6
7
8
<!-- 文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485760"/> <!-- 10m -->
<property name="maxInMemorySize" value="4096"/>
<property name="defaultEncoding" value="UTF-8">

</property>
</bean>

SpringMVC.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--Spring MVC的配置-->
<!--包扫描-->
<context:component-scan base-package="com.mmall" annotation-config="true"/>

<mvc:annotation-driven>
<mvc:message-converters>
<!--配置编码-->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!--配置JSON格式-->
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>


<!-- 文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485760"/> <!-- 10m -->
<property name="maxInMemorySize" value="4096"/>
<property name="defaultEncoding" value="UTF-8">

</property>
</bean>


</beans>

LogBack的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<!---->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoding>UTF-8</encoding>
<encoder>
<pattern>[%d{HH:mm:ss.SSS}][%p][%c{40}][%t] %m%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
</appender>

<appender name="mmall" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--<File>d:/mmalllog/mmall.log</File>-->
<File>/developer/apache-tomcat-7.0.73/logs/mmall.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/developer/apache-tomcat-7.0.73/logs/mmall.log.%d{yyyy-MM-dd}.gz</fileNamePattern>
<append>true</append>
<maxHistory>10</maxHistory>
</rollingPolicy>
<encoder>
<pattern>[%d{HH:mm:ss.SSS}][%p][%c{40}][%t] %m%n</pattern>
</encoder>
</appender>


<appender name="error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--<File>d:/mmalllog/error.log</File>-->
<File>/developer/apache-tomcat-7.0.73/logs/error.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/devsoft/apache-tomcat-7.0.73/logs/error.log.%d{yyyy-MM-dd}.gz</fileNamePattern>
<!--<fileNamePattern>d:/mmalllog/error.log.%d{yyyy-MM-dd}.gz</fileNamePattern>-->
<append>true</append>
<maxHistory>10</maxHistory>
</rollingPolicy>
<encoder>
<pattern>[%d{HH:mm:ss.SSS}][%p][%c{40}][%t] %m%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>

<logger name="com.mmall" additivity="false" level="INFO" >
<appender-ref ref="mmall" />
<appender-ref ref="console"/>
</logger>



<!-- geelynote mybatis log 日志 -->

<logger name="com.mmall.dao" level="DEBUG"/>

<!--<logger name="com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate" level="DEBUG" >-->
<!--<appender-ref ref="console"/>-->
<!--</logger>-->

<!--<logger name="java.sql.Connection" level="DEBUG">-->
<!--<appender-ref ref="console"/>-->
<!--</logger>-->
<!--<logger name="java.sql.Statement" level="DEBUG">-->
<!--<appender-ref ref="console"/>-->
<!--</logger>-->

<!--<logger name="java.sql.PreparedStatement" level="DEBUG">-->
<!--<appender-ref ref="console"/>-->
<!--</logger>-->


<root level="DEBUG">
<appender-ref ref="console"/>
<appender-ref ref="error"/>
</root>

</configuration>

Ftp服务器的配置 上传图片

1
2
3
4
5
6
ftp.server.ip=你的FTP服务器ip地址
ftp.user=mmallftp
ftp.pass=ftppassword
ftp.server.http.prefix=http://img.happymmall.com/
alipay.callback.url=http://www.happymmall.com/order/alipay_callback.do
password.salt=geelysdafaqj23ou89ZXcj@#$@#$#@KJdjklj;D../dSF.,

Idea的注入和自动编译配置

image-20190119162623139

image-20190119162630530

实时编译

image-20190119162733540

BUG

image-20190119163033651

Error:Maven Resources Compiler: Maven project configuration required for module ‘mmall’ isn’t available. Compilation of Maven projects is supported only if external build is started from an IDE.

解决方案:

image-20190119163311594

Autowired报错的解决方案

image-20190119163620744

image-20190119163632091 ## 两个提高工作效率的神器-Restlet Client和fe助手

https://restlet.com/modules/client/

https://chrome.google.com/webstore/category/extensions?utm_source=chrome-ntp-icon

image-20190119164059032

![image-20190119164218726](../../../Users/apple/Library/Application Support/typora-user-images/image-20190119164218726.png)

第五章 用户模块开发

image-20190119164448744

image-20190119164537662

image-20190119164618009

image-20190119164633294

image-20190119164659921


1 Coding

1.1 高复用服务器响应对象的设计

1
2
3
4
在web开发中,后台开发与前端交互主要是通过json的方式,后台通过统一的返回样式,可以使前后端更好的交互,在一次项目中,我每次返回一个复杂对象的时候,都是用一个匿名对象序列化成json格式的数据返回前端,由于这个项目前后台都是我一个人完成,所以我能比较清楚返回的东西,但是如果是前后端分开进行开发或者前期是一个人开发,后期进行前后端离就会变得异常困难。
因此,规范返回的格式显得很重要,于是乎我便封装了一个简单的高复可用的对象用于响应数据。具体思路如下:
---------------------
原文:https://blog.csdn.net/x1032019725/article/details/77919079
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
设计思路:
使用泛型让返回数据多样化,且可以让返回对象统一
声明状态字段,表示服务响应状态
声明描述字段,描述当前状态的原因
声明泛型数据字段,用于返回多种类型的数据,实现高复用
主要代码如下:
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.io.Serializable;

/**
* Created by *** on 2017/8/16.
*/
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
//项目使用的是Jackson包,添加该注解,保证json序列化的时候,不返回key-null这种类型的数据
public class ServerResponse<T> implements Serializable {
private int status;
private String msg;
private T data;

//私有化构造方法,通过响应状态来创建
private ServerResponse(int status){
this.status = status;
}
private ServerResponse(int status,T data){
this.status = status;
this.data = data;
}
private ServerResponse(int status,String msg,T data){
this.status = status;
this.msg = msg;
this.data = data;
}
private ServerResponse(int status,String msg){
this.status = status;
this.msg = msg;
}
@JsonIgnore
//json序列化返回前端的时候,忽略该方法,不需要返回
public boolean isSuccess() {
return this.status == ResponseCode.SUCCESS.getCode();
}
//把字段封装成属性,开放get方法
public int getStatus(){
return status;
}
public String getMsg(){
return msg;
}
public T getData(){
return data;
}
//声明创建对象的静态方法,通过下面这些方法来调用私有构造方法,并且set状态
//状态被声明在枚举ResponseCode中
public static <T> ServerResponse<T> createBySuccess(){
return new ServerResponse<T>(ResponseCode.SUCCESS.getCode());
}
public static <T> ServerResponse<T> createBySuccessMessage(String msg){
return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg);
}
public static <T> ServerResponse<T> createBySuccess(T data){
return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),data);
}
public static <T> ServerResponse<T> createBySuccess(String msg,T data){
return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg,data);
}

public static <T> ServerResponse<T> createByError(){
return new ServerResponse<T>(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDesc());
}
public static <T> ServerResponse<T> createByErrorMessage(String errorMessage){
return new ServerResponse<T>(ResponseCode.ERROR.getCode(),errorMessage);
}
public static <T> ServerResponse<T> createByErrorCodeMessage(int errorCode,String errorMessage){
return new ServerResponse<T>(errorCode,errorMessage);

}

}

下面是ResponseCode的主要代码

public enum ResponseCode {
SUCCESS(0,"SUCCESS"),
ERROR(1,"ERROR"),
NEED_LOGIN(10,"NEED_LOGIN"),
ILLEGAL_ARGUMENT(2,"ILLEGAL_ARGUMENT");

private final int code;
private final String desc;

ResponseCode(int code,String desc){
this.code = code;
this.desc = desc;
}
public int getCode(){
return this.code;
}

public String getDesc(){
return this.desc;
}

}

总结:
使用ServeResponse这个响应类去进行json序列化,然后响应前端,或者用于后台的各层之间进行交互,提高了代码的可读性,并且增加代码的重用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
高复用服务响应对象的设计与使用
一.什么是高复用服务响应对象?它有什么作用?
  1.这次项目中,为了实现前后端分离,设计了一个所有接口都使用,封装后台业务数据放回json数据给前端的对象,用于实现前后端的分离,开发效率有了明显的提高。

二.怎么使用?
  1.首先,要明确,这个对象要实现序列化接口。它主要封装了三个属性,泛型的返回数据,字符串类型的提示信息以及整型的状态码,以及四个私有的构造函数,需要注意的是,当T 的类型也就是数据类型是String类型时,好像会和下面的String msg重合,到底会调用哪一个呢?答案是,当T为String时,的确会调用第二个,这样会产生一个问题,就是当返回的数据就是String,如果这样就会用到msg的那个构造函数,传到信息那边去了。解决方法在后面,所以具体如下://保证在序列化json时,如果为空的值,key也会消失,比如只要返回状态码时,msg和data就会忽略不返回


//保证在序列化json时,如果为空的值,key也会消失
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class ServerResponse<T> implements Serializable {
private int status;
private String msg;
private T data;

private ServerResponse(int status){
this.status = status;
}
private ServerResponse(int status,String msg){
this.status = status;
this.msg = msg;
}
private ServerResponse(int status,String msg,T data){
this.status = status;
this.msg = msg;
this.data = data;
}
private ServerResponse(int status,T data){
this.data = data;}
 

2.成员变量的get方法,以及一个判断状态码或者说判断响应是否成功的方法,具体如下:


@JsonIgnore//在序列化时忽略
public boolean isSuccess(){
return this.status==ResponseCode.SUCCESS.getCode();
}
public int getStatus(){
return status;
}
public String getMsg(){
return msg;
}
public T getData(){
return data;
}


3.提供对外访问的七个构造方法,成功的有四个,失败的三个,具体如下:

复制代码
public static <T> ServerResponse<T> creatBySuccess(){
return new ServerResponse<T>(ResponseCode.SUCCESS.getCode());
}
public static <T> ServerResponse<T> creatBySuccessMessage(String msg){
return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg);
}
public static <T> ServerResponse<T> creatBySuccess(T data){
return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),data);
}
  //这个方法就解决了msg和String类型的数据冲突的问题
public static <T> ServerResponse<T> creatBySuccess(String msg,T data){
return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg,data);
}

public static <T> ServerResponse<T> creatByError(){
return new ServerResponse<T>(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDesc());
}
public static <T> ServerResponse<T> creatByErrorMessage(String errorMessage){
return new ServerResponse<T>(ResponseCode.ERROR.getCode(),errorMessage);
}
public static <T> ServerResponse<T> creatByErrorCodeMessage(int errorCode,String errorMessage){
return new ServerResponse<T>(errorCode,errorMessage);
}
复制代码
三.总结
  这次的高复用服务响应对象的设计与使用涉及到泛型类,后端的数据的处理模式,枚举类的使用,以及前后端数据交互等知识,在后期使用Restlet Clint进行接口功能测试时更加直观地看到了这个对象的作用。

https://blog.csdn.net/qq_27093465/article/details/52180865